package com.fairytask.objects
{
	import com.fairytask.define.ResDefine;
	import com.fairytask.resource.ResManager;
	import com.fairytask.scene.BattleScene;
	import com.fairytask.utils.Vec2D;
	
	import flash.geom.ColorTransform;
	
	import starling.display.Image;
	
	public class Virus extends Avatar
	{
		internal var _scaleYMin:Number=0.4;
		
		internal var _scaleTime:Number=0.3;
		
		internal var _scaleYMax:Number=1.2;
		
		public var feelerDist:uint=2000;
		
		internal var _toCell:Cell;
		
		internal var _scaleXMin:Number=0.8;
		
		internal var _scalingOut:Boolean=true;
		
		internal var _maxAngVel:uint=600;
		
		public var fromCell:Cell;
		
		public var value:Number=1;
		
		internal var _mc:Image;
		
		internal var _scaleXMax:Number=2.3;
		
		internal var _maxVel:uint=100;
		
		internal var _offset:Vec2D;
		
		internal var _unitType:int=-1;
		
		public function Virus()
		{
			_offset = new Vec2D();
			super();
			//TODO 要设置个皮肤
			_mc = new Image(ResManager.getAtlasTexture(ResDefine.POP_SHEET,"15"));
			var randomSize:Number=Math.random();
			_mc.scaleX = _scaleXMin + (_scaleXMax - _scaleXMin) * randomSize;
			_mc.scaleY = _scaleYMax - (_scaleYMax - _scaleYMin) * randomSize;
			addChild(_mc);
		}
		
		public function set toCell(arg1:Cell):void
		{
			var loc1:*=null;
			_toCell = arg1;
			loc1 = _toCell.vec;
			loc1.x = loc1.x + (-_toCell.radius * 0.6 + Math.random() * _toCell.radius * 1.2);
			loc1.y = loc1.y + (-_toCell.radius * 0.6 + Math.random() * _toCell.radius * 1.2);
			_offset.x = _toCell.x - loc1.x;
			_offset.y = _toCell.y - loc1.y;
			pointAt(_toCell.x, _toCell.y);
		}
		
		public function updateMove(arg1:Number):void
		{
			if (!_toCell) 
			{
				return;
			}
			var loc1:*=_toCell.vec.addVec(_offset);
			var loc2:*=loc1.subVec(posV).normalized;
			var loc3:*=0;
			var loc4:*=0;
			loc3 = arg1 / _scaleTime * (_scaleXMax - _scaleXMin);
			loc4 = arg1 / _scaleTime * (_scaleYMax - _scaleYMin);
			if (_scalingOut) 
			{
				_mc.scaleX = _mc.scaleX + loc3;
				_mc.scaleY = _mc.scaleY - loc4;
				if (_mc.scaleX >= _scaleXMax) 
				{
					_scalingOut = false;
					_mc.scaleX = _scaleXMax;
					_mc.scaleY = _scaleYMin;
				}
				vel = (_mc.scaleX - _scaleXMin) / (_scaleXMax - _scaleXMin) * _maxVel / 2;
			}
			else 
			{
				_mc.scaleX = _mc.scaleX - loc3;
				_mc.scaleY = _mc.scaleY + loc4;
				if (_mc.scaleX <= _scaleXMin) 
				{
					_scalingOut = true;
					_mc.scaleX = _scaleXMin;
					_mc.scaleY = _scaleYMax;
				}
				vel = (_mc.scaleX - _scaleXMin) / (_scaleXMax - _scaleXMin) * _maxVel;
			}
			angVel = ov.normal.dot(loc2) * _maxAngVel;
		}
		
		public function get toCell():Cell
		{
			return _toCell;
		}
		
		public function get unitType():int
		{
			return _unitType;
		}
		
		public function veerFrom(arg1:Vec2D, arg2:Number, arg3:Number):void
		{
			angVel = angVel + (ov.isLeft(arg1) ? -1 : 1) * _maxVel * (arg2 + feelerDist) / arg3;
		}
		
		public function set unitType(arg1:int):void
		{
			_unitType = arg1;
			var unitType:*=BattleScene.getUnitType(_unitType);
			var loc2:ColorTransform=new ColorTransform();
			loc2.color = unitType.color;
			//_mc.transform.colorTransform = loc2;
			_maxVel = unitType.speed;
			_maxAngVel = _maxVel * 3;
			_scaleTime = unitType.agility;
			value = unitType.value;
		}
	}
}